﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI.DataTypes;

namespace ASPNETVN.PORTAL.Components.UI
{
    public class PortalModuleControl : UserControl
    {
        //-----------------------------------------------------------------------------------------
        private readonly Hashtable _BaseSettings = new Hashtable();
        private Module _Module;
        private Hashtable _Settings;

        private readonly PlaceHolder _header = new PlaceHolder();
        private readonly PlaceHolder _footer = new PlaceHolder();
        private readonly PlaceHolder _headerPlaceHolder = new PlaceHolder();

        public ModuleTitle ModuleTitle;

        #region Tab

        private int _tabID;
        private int _tabIndex;

        public int TabID
        {
            get
            {
                if (this._tabID == 0)
                {
                    if (HttpContext.Current != null && this.Request.Params["TabID"] != null)
                        this._tabID = Int32.Parse(this.Request.Params["TabID"]);
                    else
                        this._tabID = 1;
                }
                return this._tabID;
            }
        }

        public int TabIndex
        {
            get
            {
                if (this._tabIndex == 0)
                {
                    if (HttpContext.Current != null && this.Request.Params["TabIndex"] != null)
                        this._tabIndex = Int32.Parse(this.Request.Params["TabIndex"]);
                }
                return this._tabIndex;
            }
        }

        #endregion

        #region Module

        public int ModuleID
        {
            get { return this._Module.ID; }
        }

        public Guid GeneralModuleDefinitionID
        {
            get { return this._Module.GeneralDefinitionID; }
        }

        #endregion

        #region Portal

        public Configuration.Items.Portal Portal
        {
            get { return (Configuration.Items.Portal)HttpContext.Current.Items["Portal"]; }
        }

        #endregion

        #region Permission

        private int _isAddable;
        private int _isDeleteable;
        private int _isEditable;
        private int _isManagerable;
        private int _isViewable;

        public bool IsViewable
        {
            get
            {
                // Administrators Role.
                if (PortalSecurity.IsInRole("1")) return true;

                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)
                if (this._Module == null || this._Module.AuthorizedViewRoles == null)
                    return false;

                if (this._isViewable == 0)
                {
                    // Obtain Portal from Current Context
                    if (PortalSecurity.IsInRoles(this._Module.AuthorizedViewRoles))
                    {
                        this._isViewable = 1;
                    }
                    else
                    {
                        this._isViewable = 2;
                    }
                }

                return (this._isViewable == 1);
            }
        }

        //-----------------------------------------------------------------------------------------

        public bool IsAddable
        {
            get
            {
                // Administrators Role.
                if (PortalSecurity.IsInRole("1")) return true;

                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (this._isAddable == 0)
                {
                    // Obtain Portal from Current Context
                    Configuration.Items.Portal pSettings = (Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

                    if (pSettings.AlwaysShowEditButton && PortalSecurity.IsInRoles(this._Module.AuthorizedAddRoles))
                    {
                        this._isAddable = 1;
                    }
                    else
                    {
                        this._isAddable = 2;
                    }
                }

                return (this._isAddable == 1);
            }
        }

        //-----------------------------------------------------------------------------------------

        public bool IsEditable
        {
            get
            {
                // Administrators Role.
                if (PortalSecurity.IsInRole("1")) return true;

                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (this._isEditable == 0)
                {
                    // Obtain Portal from Current Context
                    Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

                    if (portal.AlwaysShowEditButton && PortalSecurity.IsInRoles(this._Module.AuthorizedEditRoles))
                    {
                        this._isEditable = 1;
                    }
                    else
                    {
                        this._isEditable = 2;
                    }
                }

                return (this._isEditable == 1);
            }
        }

        //-----------------------------------------------------------------------------------------

        public bool IsDeleteable
        {
            get
            {
                // Administrators Role.
                if (PortalSecurity.IsInRole("1")) return true;

                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (this._isDeleteable == 0)
                {
                    // Obtain Portal from Current Context
                    Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

                    if (portal.AlwaysShowEditButton && PortalSecurity.IsInRoles(this._Module.AuthorizedDeleteRoles))
                    {
                        this._isDeleteable = 1;
                    }
                    else
                    {
                        this._isDeleteable = 2;
                    }
                }

                return (this._isDeleteable == 1);
            }
        }

        //-----------------------------------------------------------------------------------------

        public bool IsManagerable
        {
            get
            {
                // Administrators Role.
                if (PortalSecurity.IsInRole("1")) return true;

                // Perform tri-state switch check to avoid having to perform a security
                // role lookup on every property access (instead caching the result)

                if (this._isManagerable == 0)
                {
                    // Obtain Portal from Current Context
                    Components.Configuration.Items.Portal pSettings = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

                    if (pSettings.AlwaysShowEditButton && PortalSecurity.IsInRoles(this._Module.AuthorizedManagerRoles))
                    {
                        this._isManagerable = 1;
                    }
                    else
                    {
                        this._isManagerable = 2;
                    }
                }

                return (this._isManagerable == 1);
            }
        }

        #endregion

        public PortalModuleControl()
        {
            ModuleSettingItem showTitle = new ModuleSettingItem(new BooleanDataType(), "true");
            showTitle.Name = "Hiển thị tiêu đề";
            showTitle.Description = "Hiển thị tiêu đề của module";
            showTitle.Group = ModuleSettingItemGroup.Module_Special;
            this.BaseSettings.Add("ShowTitle", showTitle);
        }

        public Module Module
        {
            get { return this._Module; }
            set { this._Module = value; }
        }

        public Hashtable BaseSettings
        {
            get { return this._BaseSettings; }
        }

        public Hashtable Settings
        {
            get
            {
                if (this._Settings == null)
                {
                    this._Settings = PortalConfiguration.GetModuleSettings(this.ModuleID, this._BaseSettings);
                }

                return this._Settings;
            }
        }


        public virtual Guid GuidID
        {
            get { throw new NotImplementedException("You must implement a unique GUID for your module"); }
        }

        #region Event

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Create frame for module content.
            LiteralControl literalControl12345 = new LiteralControl();
            literalControl12345.Text = "<div class='ModuleFrame'><table width='100%' cellpadding='0' cellspacing='0' border='0'><tr><td class='ModuleFrame1'></td><td class='ModuleFrame2'></td><td class='ModuleFrame3'></td></tr><tr><td class='ModuleFrame4'></td><td class='ModuleFrame5'>";

            LiteralControl literalControl6789 = new LiteralControl();
            literalControl6789.Text = "</td><td class='ModuleFrame6'></td></tr><tr><td class='ModuleFrame7'></td><td class='ModuleFrame8'></td><td class='ModuleFrame9'></td></tr></table></div>";

            _header.Controls.Add(literalControl12345);
            _footer.Controls.Add(literalControl6789);

            this._headerPlaceHolder.Controls.Add(_header);
            this.Controls.Add(_footer);


        }
        protected override void OnInit(EventArgs e)
        {
            this.ModuleTitle = (ModuleTitle) this.LoadControl("~/ModuleTitle.ascx");
            this.ModuleTitle.StandardSettingUrl = "~/Administration/Module/StandardSetting.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID + "&mid=" + this.ModuleID;
            this.ModuleTitle.CustomSettingUrl = "~/Administration/Module/CustomSetting.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID + "&mid=" + this.ModuleID;
            this.ModuleTitle.StandardPermissionUrl = "~/Administration/Module/StandardPermission.aspx?tabindex=" + this.TabIndex + "&tabid=" + this.TabID + "&mid=" + this.ModuleID;
            
            
            this.Controls.AddAt(0, this.ModuleTitle);
            this.Controls.AddAt(1, _headerPlaceHolder);

            base.OnInit(e);
        }

        #endregion
    }
}